package myNetty.action4;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


/**
 * @author tonywang
 * @date 7/25/2018
 */
public class MultipleWrite {
    public void multipleWrite(){
        final Channel channel = new NioSocketChannel();
        //Create ByteBuf that holds data to write
        final ByteBuf buf = Unpooled.copiedBuffer("your data", CharsetUtil.UTF_8);
        //Create Runnable which writes data to channel
        Runnable writer = new Runnable() {
            @Override
            public void run() {
                channel.write(buf.duplicate());
            }
        };
        //Obtain reference to the Executor which uses threads to execute tasks
        Executor executor = Executors.newCachedThreadPool();
        // write in one thread
        //Hand over write task to executor for execution in thread
        executor.execute(writer);
        // write in another thread
        //Hand over another write task to executor for execution in thread
        executor.execute(writer);
    }
}
